package org.ykh.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ykh
 */
@Configuration
public class GateWay {

    @Bean
    public CorsWebFilter corsWebFilter(){
        System.out.println("=====================================");
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("http://gmall.frp.ykh.net.cn");
        corsConfiguration.addAllowedOrigin("https://gmall.frp.ykh.net.cn");
        corsConfiguration.addAllowedOrigin("http://localhost");
        corsConfiguration.addAllowedOrigin("https://localhost");
        corsConfiguration.addAllowedOrigin("http://127.0.0.1");
        corsConfiguration.addAllowedOrigin("https://127.0.0.1");
//        corsConfiguration.addAllowedOriginPattern("*");
//        corsConfiguration.setAllowCredentials(true);//允许携带cookie,
//        为true一定要addAllowedOrigin（单一具体来源）或addAllowedOriginPattern（允许通配符，和正则）设置具体设置来源，
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //注册配置
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
